﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IBS;
using System.IO;

namespace HQCSupplierFileParser
{
    public class Parser
    {
        private string path { get; set; }

        public Parser(string path)
        {
            this.path = path;
        }

        public List<HQCSUPPLIER_CODE> parseFile()
        {
            string line;
            int counter = 1;
            List<HQCSUPPLIER_CODE> hqcSupplierList = new List<HQCSUPPLIER_CODE>();
            using (System.IO.StreamReader file = new StreamReader(this.path))
            {
                while ((line = file.ReadLine()) != null)
                {
                    if (counter > 5)//парсим с 6 строки
                    {
                        //код станции
                        //короткий код поставщика
                        //длинный код поставщика
                        if (line.Length == 109)
                        {
                            if (line.Substring(66, 20).Trim() != "<null>")
                            {
                                HQCSUPPLIER_CODE suppLine = new HQCSUPPLIER_CODE();
                                suppLine.SITECODE = Int64.Parse(line.Substring(0, 12).Trim());
                                suppLine.HQC_SHORTCODE = Int32.Parse(line.Substring(62, 4).Trim());
                                suppLine.HQC_LONGCODE = Int64.Parse(line.Substring(66, 20).Trim());
                                hqcSupplierList.Add(suppLine);
                            }
                        }                                                
                    }                   
                    counter++;                    
                }
                file.Close();
            }
            return hqcSupplierList;        
        }
    }
}
